<?php
// @formatter:off
/**
 * @file WCRepresentation.abs.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 19:41:09 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: WCRepresentation.abs.php 72 2013-06-16 19:41:09Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/includes/representations/WCRepresentation.abs.php $
 */
// @formatter:on

/**
 * @abstract
 * @class WCRepresentation
 */
abstract class WCRepresentation {
	protected $_PropertiesReadOnly = array("id");
	protected $_PropertiesBoolean = array();
	protected $_PropertiesPrefix = "";
	protected $_PropertiesTable = "";
	protected $_PropertiesNameColumn = "name";
	protected $_PropertiesAutoSet = "";
	protected $_PropertiesNULL = array();
	protected $_PropertiesPresetFunc = array();

	/**
	 * @var WCConnection
	 */
	protected $_db;
	/**
	 * @var string
	 */
	protected $_dbprefix;
	/**
	 * @var array
	 */
	protected $_info = array();
	/**
	 * @var bool
	 */
	protected $_ok;

	public function __construct() {
		global $wcDatabaseAccess;

		$this->_ok = false;

		$this->_db = WCConnection::Instance();
		$this->_dbprefix = $wcDatabaseAccess['prefix'];
	}
	//
	// Public Methods
	public function __get($name) {
		$property = $this->_PropertiesPrefix.$name;

		if(!isset($this->_info[$property]) && in_array($name, $this->_PropertiesNULL)) {
			$this->_info[$property] = false;
		}

		return isset($this->_info[$property]) ? $this->_info[$property] : false;
	}
	public function __set($name, $value) {
		$out = false;

		if($this->ok() && !in_array($name, $this->_PropertiesReadOnly)) {
			$property = $this->_PropertiesPrefix.$name;

			if(!isset($this->_info[$property]) && in_array($name, $this->_PropertiesNULL)) {
				$this->_info[$property] = false;
			}

			if(isset($this->_info[$property])) {
				$stmt = false;
				$stmtID = __CLASS__."::".__FUNCTION__."[{$property}]";
				if($this->_db->has($stmtID)) {
					$stmt = $this->_db->get($stmtID);
				} else {
					$prefix = $this->_PropertiesPrefix;
					$table = $this->_dbprefix.$this->_PropertiesTable;

					$query = "update  {$table}\n";
					$query .= "set     {$property} = :value{$this->_PropertiesAutoSet}\n";
					$query .= "where   {$prefix}id = :id\n";
					$stmt = $this->_db->prepare($stmtID, $query);
				}

				//
				// Boolean values
				$realValue = $value;
				if(in_array($name, $this->_PropertiesBoolean)) {
					$realValue = $value ? "Y" : "N";
				}

				if(isset($this->_PropertiesPresetFunc[$name])) {
					eval("\$realValue = {$this->_PropertiesPresetFunc[$name]}(\$realValue);");
				}

				$params = array(
					":value" => $realValue,
					":id" => $this->id
				);

				if($this->subSet($name, $value)) {
					if($stmt->execute($params)) {
						$this->_info[$property] = $value;
						$out = true;
					}
				}
			} else {
				trigger_error(__CLASS__.": Property '{$name}' does not exists", E_USER_WARNING);
			}
		} elseif(!$this->ok()) {
			trigger_error(__CLASS__.": Object is not loaded", E_USER_WARNING);
		} else {
			trigger_error(__CLASS__.": Property '{$name}' cannot be modified", E_USER_WARNING);
		}

		return $out;
	}
	public function id() {
		return $this->id;
	}
	public function name() {
		eval("\$name = \$this->{$this->_PropertiesNameColumn};");
		return $name;
	}
	public function ok() {
		return $this->_ok;
	}
	public function reload() {
		return false;
	}
	public function remove() {
		$out = false;

		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__."[".$this->_PropertiesTable."]";
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$prefix = $this->_PropertiesPrefix;
			$table = $this->_dbprefix.$this->_PropertiesTable;

			$query = "delete from {$table}\n";
			$query .= "where       {$prefix}id = :id\n";
			$stmt = $this->_db->prepare($stmtId, $query);
		}

		$params = array(":id" => $this->id);

		if($stmt->execute($params)) {
			$this->_ok = false;
			$this->_info = array();

			$out = true;
		}

		return $out;
	}
	//
	// Protected Methods
	protected function subSet($name, &$value) {
		return true;
	}
}

interface WCPreviews {
	public function generatePreviews();
	public function hasPreview();
	public function hasPreviews();
	public function preview();
	public function previews();
}
?>